package com.wzp.util.lru;

/**
 * @author zhengpeng.wang
 */

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExample {
    private static final int THREAD_COUNT = 100;
    private static final int TASK_COUNT = 10000;

    public static void main(String[] args) {
        LRUCache cache = new LRUCache(3000); // 假设LRUCache的容量为1000
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>());

        // 向LRUCache中放入一些数据，使得缓存命中率不为0
        for (int i = 0; i < 30000; i++) {
            cache.put(i, i);
        }

        long startTime1 = System.currentTimeMillis();
        // 提交任务到线程池
        for (int i = 0; i < TASK_COUNT; i++) {
            int finalI = i;
            threadPool.execute(() -> {
                long startTime = System.currentTimeMillis();
                int key = (int) (Math.random() * 30000); // 随机生成一个key
                int value = cache.get(key); // 调用LRUCache的get方法
                System.out.println("i: " + finalI + ", Key: " + key + ", Value: " + value + ", costTime = " + (System.currentTimeMillis() - startTime));
            });
        }

        System.out.println("costTime1 = " + (System.currentTimeMillis() - startTime1));
        // 关闭线程池
        threadPool.shutdown();

    }
}

